﻿using ApiLib.Models;
using ApiLib.Utils;
using Furion.Schedule;

namespace ApiLib.Cloud.Common;

/// <summary>
/// 
/// </summary>
[PeriodMinutes(10, RunOnStart = true)]
public class CloudRegisterJob : IJob
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <param name="stoppingToken"></param>
    public Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        CloudUtil.InitConfig();
        if (MyCloudConstants.Config==null)
        {
            return Task.CompletedTask;
        }
        
        if (MyCloudConstants.CloudType == CloudModalType.Service)
        {
            var response =
                 HttpUtil.HttpPost<BaseResponse<string>>(MyCloudConstants.Url.Register, null, MyCloudConstants.Config);
            if (response is { code: 0 })
            {
                WebUtil.LogSys($"注册结果：{response.data}");
            }
            else
            {
                WebUtil.LogExp($"注册结果：{response?.msg ?? ""}");
            }
        }
        else
        {
            //更新路由
            CloudUtil.GetServiceInfo(true);
        }

        return Task.CompletedTask;
    }
}